;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Sales.GiftList", [])
        .controller("GiftListCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$timeout", "EventBus", "$filter", "UserContextService", "$stateParams", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $timeout, EventBus, $filter, UserContextService, $stateParams) {
            $scope.activeFlag = [true];
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.condition = {};
            $scope.toolbarConfig = {
                showAdvanceSearch: false,
                showColConfig: false,
                showMateriel: false
            };
            $scope.dataList = [];

            $scope.tabFlag = {};

            $scope.exportParam = {
                sectionUrl: ybHttp.RequestUrl("device", "/giftticket/export"),
                sectionParam: {
                    ids: []
                },
                allUrl: ybHttp.RequestUrl("device", "/giftticket/masterialsExport")
            };

            $scope.tableConfig = {
                scrollWidth: 720,
                // fixedWidth: 200,
                tableCol: [{
                        type: "",
                        name: "零件编号",
                        dataField: "materialsNo",
                        width: 200,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "零件中文名",
                        dataField: "materialsDes",
                        width: 200,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "机型",
                        dataField: "machineType",
                        width: 200,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "单位",
                        dataField: "unit",
                        width: 200,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "价格",
                        dataField: "price",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                ]
            };

            var btnPermissionCode = {
                giftListExport: window.ActionCode.giftListExport,
                giftListImport: window.ActionCode.giftListImport,
            };

            $scope.btnPermission = {
                giftListExport: true,
                giftListImport:true,
            };

            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }

            $scope.setFixedWidth = function () {
                $scope.tbStyle = {
                    billFixedWidth: {
                        left: $scope.tableConfig.fixedWidth + "px"
                    }
                };
            };

            $scope.changeTab = function (index) {
                $scope.activeFlag = [false, false, false];
                $scope.activeFlag[index] = true;
                $scope.submitted = false;
                switch (index) {
                    case 0:
                        getDataList();
                        $scope.submitted = false;
                        break;
                    default:
                        break;
                }
            };


            function getDataList(curPage) {
                $scope.exportParam.sectionParam.ids = [];
                var service = "device";
                var reqUrl = "/giftticket/masterials";
                var param = {};
                param.pageSize = $scope.pageInfo.pageSize;
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                param.pageNum = $scope.pageInfo.curPage;
                param.filters = $scope.condition;
                return ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;
                    return resp.value.list;
                });
            }

            // $scope.tdClick = function (item, fnType) {
            //     switch (fnType) {
            //         case "viewDetail":
            //             viewDetail(item);
            //             break;
            //         default:
            //             break;
            //     }
            // };


            $scope.toSort = function (config, index) {
                ybWidget.ToSort(config, index);
            };

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };

            $scope.reset = function () {
                $scope.condition = {
                    seller: true
                };
            };

            $scope.import = function () {
                var param = {
                    name: "导入赠送件明细",
                    templateReqService: "device",
                    templateReqUrl: "/giftticket/downloadImportExcel",
                    importReqService: "device",
                    importReqUrl: "/giftticket/materialsImport",
                    importErrorUrl: "",
                    templateType: "giftList"
                };
                ybWidget.ImportFile(param).then(function (data) {
                    getDataList(1);
                });
            };

            (function () {
                getDataList();
                $scope.setFixedWidth();
                UserContextService.SetBtnPermission(setBtnPermission);
            })();
        }]);
})(window, window.angular);
